package com.jack.payment.service.mongo;

import com.jack.payment.entity.filter.RestFilter;
import com.jack.payment.rabbitmq.entity.QueueContentModel;
import com.jack.payment.util.common.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class QueueLogServiceImpl {
    private Logger log = LoggerFactory.getLogger(QueueLogServiceImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 写入队列消息
     * @param queueContentModel
     * @param collectionName
     */
    public void saveQueueLog(QueueContentModel queueContentModel, String collectionName) {
        queueContentModel.setDbName(null);
        mongoTemplate.insert(queueContentModel, collectionName);
    }

    /**
     * 修改队列消息
     * @param queueContentModel
     * @param collectionName
     */
    public void updateQueueLog(QueueContentModel queueContentModel, String collectionName) {
        Query query = new Query(Criteria.where("_id").is(queueContentModel.getId()));
        Update update = new Update();
        if (queueContentModel.getStatus() != null) {
            update.set("status", queueContentModel.getStatus());
        }
        if (queueContentModel.getCount() != null) {
            update.set("count", queueContentModel.getCount());
        }
        if (queueContentModel.getSuccessDate() != null) {
            update.set("successDate", queueContentModel.getSuccessDate());
        }
        if (queueContentModel.getLastDate() != null) {
            update.set("lastDate", queueContentModel.getLastDate());
        }
        if (!StringUtil.isEmpty(queueContentModel.getExceptionInfo())) {
            update.set("exceptionInfo", queueContentModel.getExceptionInfo());
        }
        mongoTemplate.updateFirst(query, update, collectionName);
    }



    /**
     * 筛选mq 日志列表
     * @param filterModel
     * @return
     */
    public List<QueueContentModel> filterMQLogRecordList(RestFilter.QueueFilter filterModel) {
        Sort.Direction direction  = filterModel.getSort() == null ? Sort.Direction.DESC :
                filterModel.getSort() == 0 ? Sort.Direction.DESC : Sort.Direction.ASC;
        Sort sort = Sort.by(direction, "lastDate");
        Query query = requestMQCondition(filterModel);
        Pageable pageable = PageRequest.of(filterModel.getPage(), filterModel.getCount(), sort);
        query.with(pageable);
        List<QueueContentModel> pageList = mongoTemplate.find(query, QueueContentModel.class, filterModel.getDbName());
        return pageList;
    }

    /**
     * 筛选MQ日志 总数
     * @param filterModel
     * @return
     */
    public Integer filterMQLogRecordCount(RestFilter.QueueFilter filterModel) {
        Query query = requestMQCondition(filterModel);
        return (int)mongoTemplate.count(query, QueueContentModel.class, filterModel.getDbName());

    }


    /**
     * 根据id 获取mq 日志 信息
     * @param id
     * @return
     */
    public QueueContentModel getAppointIdQueueContent(String id, String tableName) {
        return mongoTemplate.findById(id, QueueContentModel.class, tableName);
    }


    private Query requestMQCondition(RestFilter.QueueFilter filterModel) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (filterModel.getStatus() != null) {
            criteria.and("status").is(filterModel.getStatus());
        }
        if (filterModel.getIsManual() != null) {
            criteria.and("isManual").is(filterModel.getIsManual());
        }
        if (filterModel.getSubject() != null) {
            criteria.and("subject").is(filterModel.getSubject());
        }
        if (filterModel.getFilterStartTime() != null) {
            // 筛选 开时间
            criteria.and("lastDate").gte(filterModel.getFilterStartTime());
        }
        if (filterModel.getFilterEndTime() != null) {
            // 筛选结束时间
            criteria.and("lastDate").lte(filterModel.getFilterEndTime());
        }
        query.addCriteria(criteria);
        return query;
    }
}
